package com.sinosoft.common.manage.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinosoft.common.manage.domain.GoodsUnderDiseaseInfo;
import com.sinosoft.common.manage.mapper.GoodsUnderDiseaseInfoMapper;
import com.sinosoft.common.manage.service.GoodsUnderDiseaseInfoService;
import com.sinosoft.common.manage.vo.DiseaseVO;
import com.sinosoft.common.manage.vo.GoodsUnderDiseaseInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Yeung
* @description 针对表【goods_under_disease_info(核保病种信息表)】的数据库操作Service实现
* @createDate 2024-04-24 10:15:14
*/
@Service
@Slf4j
public class GoodsUnderDiseaseInfoServiceImpl extends ServiceImpl<GoodsUnderDiseaseInfoMapper, GoodsUnderDiseaseInfo>
    implements GoodsUnderDiseaseInfoService{

    @Resource
    private GoodsUnderDiseaseInfoMapper goodsUnderDiseaseInfoMapper;

    @Override
    public List<DiseaseVO> queryDiseaseAll() {
        try {
            List<DiseaseVO> diseaseVos = new ArrayList<>();
            List<String> mainDiseaseNameList = new ArrayList<>();
            List<GoodsUnderDiseaseInfoVO> goodsUnderDiseaseInfoVOList = new ArrayList<>();

            List<GoodsUnderDiseaseInfo> goodsUnderDiseaseInfos = goodsUnderDiseaseInfoMapper.queryDiseaseAll();
            //组装一级病种列表和二级病种列表
            for (GoodsUnderDiseaseInfo goodsUnderDiseaseInfo : goodsUnderDiseaseInfos) {
                mainDiseaseNameList.add(goodsUnderDiseaseInfo.getMainDiseaseName());
                GoodsUnderDiseaseInfoVO goodsUnderDiseaseInfoVO = new GoodsUnderDiseaseInfoVO();
                BeanUtils.copyProperties(goodsUnderDiseaseInfo,goodsUnderDiseaseInfoVO);
                goodsUnderDiseaseInfoVOList.add(goodsUnderDiseaseInfoVO);
            }
            //一级病种列表去重
            List<String> mainDiseaseNames= mainDiseaseNameList.stream().distinct().collect(Collectors.toList());
            //组装病种关联列表
            for (String mainDiseaseName : mainDiseaseNames) {
                DiseaseVO diseaseVO = new DiseaseVO();
                List<GoodsUnderDiseaseInfoVO> goodsUnderDiseaseInfoVos = new ArrayList<>();
                String mainDiseaseCode = "";
                for (GoodsUnderDiseaseInfoVO goodsUnderDiseaseInfoVO : goodsUnderDiseaseInfoVOList) {
                    if (mainDiseaseName.equals(goodsUnderDiseaseInfoVO.getMainDiseaseName())){
                        goodsUnderDiseaseInfoVos.add(goodsUnderDiseaseInfoVO);
                        mainDiseaseCode = goodsUnderDiseaseInfoVO.getMainDiseaseCode();
                    }
                }
                diseaseVO.setMainDiseaseName(mainDiseaseName);
                diseaseVO.setMainDiseaseCode(mainDiseaseCode);
                diseaseVO.setGoodsUnderDiseaseInfoVOList(goodsUnderDiseaseInfoVos);
                diseaseVos.add(diseaseVO);
            }
            log.info("com.sinosoft.common.manage.service.impl.GoodsUnderDiseaseConfigServiceImpl.queryByGoodsId 返回={}", JSON.toJSONString(diseaseVos));

            return diseaseVos;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

}




